﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.Other
{
    public partial class FrmFordBoxCheck : Form
    {
        public FrmFordBoxCheck()
        {
            InitializeComponent();
        }

        private void clearBillLbl()
        {
            areaMsg.Text = "条码格式不正确，请确认！";
            txtBillLablel.Text = "";
            txtBillLablel.Focus();
        }
        private void clearBoxNo()
        {
            areaMsg.Text = "条码格式不正确，请确认！";
            this.txtBoxNo.Text = "";
            this.txtBoxNo.Focus();
        }
        private string _s7DC = "";
        private string _s8PN = "";
        private string _s9LOC1 = "";
        private string _s9LOC2 = "";

        private void txtBillLablel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                _s7DC = "";
                _s8PN = "";
                _s9LOC1 = "";
                _s9LOC2 = "";

                var sql = string.Format("SELECT * FROM wms_T_OutStock WHERE BillNo='{0}';", this.txtBillLablel.Text);
                var dt = Utils.NMS.GetDataTable(sql);
                if (dt.Rows.Count == 0)
                {
                    clearBillLbl();
                    areaMsg.Text = "订单不存在";
                }
                var sono = dt.Rows[0]["RelatedBill"].ToString().Trim();
                var solineno = dt.Rows[0]["RowNo"].ToString().Trim();
                var seq = dt.Rows[0]["SN"].ToString().Trim();

                sql = @"SELECT  * FROM OPENQUERY(ERPBU_LINK, 
                'select * from wms_ttddev523105view where sono=''{0}'' and solineno=''{1}'' and seq=''{2}''')
                 AS derivedtbl_1";
                sql = string.Format(sql, sono, solineno, seq);
                var dtErp = Utils.NMS.GetDataTable(sql);
                if (dtErp.Rows.Count == 0)
                {
                    sql = @"SELECT  * FROM OPENQUERY(ERPBU_LINK, 
                'select * from wms_ttddev523105view where sono=''{0}'' and solineno=''{1}'' ORDER BY seq DESC')
                 AS derivedtbl_1";
                    sql = string.Format(sql, sono, solineno);
                    dtErp = Utils.NMS.GetDataTable(sql);
                    if (dtErp.Rows.Count == 0)
                    {
                        clearBillLbl();
                        areaMsg.Text = "没有查询到关联信息";
                        return;
                    }
                }

                _s7DC = dtErp.Rows[0]["t_rece"].ToString().Trim();
                _s8PN = dtErp.Rows[0]["item"].ToString().Trim();
                _s9LOC1 = dtErp.Rows[0]["t_rslf"].ToString().Trim();
                _s9LOC2 = dtErp.Rows[0]["t_deli"].ToString().Trim();
                this.txtBoxNo.Focus();
            }
            catch
            {
                clearBillLbl();
                areaMsg.Text = "异常";
                return;
            }
        }

        private void txtBoxNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            areaInfo.Text = "";
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                var sql = string.Format("SELECT * FROM wms_T_OutStockRowDetails WHERE BillNo='{0}' and BoxNo='{1}';",
                    this.txtBillLablel.Text, txtBoxNo.Text);
                var dt = Utils.NMS.GetDataTable(sql);
                if (dt.Rows.Count == 0)
                {
                    clearBoxNo();
                    areaMsg.Text = "箱号不存在";
                    return;
                }

                var partNumber = dt.Rows[0]["PartNumber"].ToString().Trim();
                if (_s8PN.Contains(partNumber))
                {
                    areaInfo.Text = "Validation \r\nOK ";
                }
                else
                {
                    areaInfo.Text = "Validation \r\nNG";
                    MessageBox.Show("NG");
                }
                clearBillLbl();
                clearBoxNo();
                areaMsg.Text = "";
            }
            catch
            {
                clearBoxNo();
                return;
            }
        }
        //END
    }
}